package com.zf.shuashua.ui.mine;


import android.content.Intent;
import android.text.TextUtils;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareConfig;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.vise.xsnow.http.callback.ACallback;
import com.zf.shuashua.helper.LoadDialogManager;
import com.zf.shuashua.R;
import com.zf.shuashua.api.HttpData;
import com.zf.shuashua.base.BaseActivity1;
import com.zf.shuashua.bean.BindWX;
import com.zf.shuashua.bean.Me;
import com.zf.shuashua.utils.ToastUtil;

import java.util.Map;

public class AccountManagement extends BaseActivity1 {
    private Toolbar mToolbar;
    private TextView mTvTitle;
    private LinearLayout llPhone;
    private LinearLayout llWx;
    private LinearLayout llQq;
    private LinearLayout llSina;
    private TextView tvPhone;
    private TextView tvQq;
    private TextView tvWx;
    private TextView tvSina;
    private MineFragmVM VM;
    private String type = "";
    UMAuthListener authListener = new UMAuthListener() {
        /**
         * @desc 授权开始的回调
         * @param platform 平台名称
         */
        @Override
        public void onStart(SHARE_MEDIA platform) {
        }

        /**
         * @desc 授权成功的回调
         * @param platform 平台名称
         * @param action 行为序号，开发者用不上
         * @param data 用户资料返回
         */
        @Override
        public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {
            String open_id = data.get("uid");
            if (open_id == null) {
                open_id = "";
            }
            String nick_name = data.get("name");
            if (nick_name == null) {
                nick_name = "";
            }
            String avatar = data.get("iconurl");
            if (avatar == null) {
                avatar = "";
            }
            String sex = data.get("gender");
            if (sex == null) {
                sex = "";
            }
            String city = data.get("city");
            if (city == null) {
                city = "";
            }
            if (SHARE_MEDIA.QQ == platform) {
                type = "qq";
            } else if (SHARE_MEDIA.SINA == platform) {
                type = "weibo";
            } else if (SHARE_MEDIA.WEIXIN == platform) {
                if (isActivityEnable()) {
                    LoadDialogManager.getInstance().show(AccountManagement.this);
                }
                type = "wechat";
                open_id = data.get("openid");
                if (open_id == null) {
                    open_id = "";
                }
            }
            HttpData.getInstance().bindThird(type, open_id, nick_name, avatar, sex, city, "", "", "", "", new ACallback<BindWX>() {
                @Override
                public void onSuccess(BindWX data) {
                    LoadDialogManager.getInstance().dismiss();
                    if (data != null) {
                        if (data.isStatus()) {
                            VM.getData();
                            ToastUtil.show(AccountManagement.this, data.getData() + "");
                        } else {
                            ToastUtil.show(AccountManagement.this, data.getMessage() + "");
                        }
                    }
                }

                @Override
                public void onFail(int errCode, String errMsg) {
                    LoadDialogManager.getInstance().dismiss();
                    ToastUtil.showToast(AccountManagement.this, "绑定失败,请重试");
                }
            });
        }

        /**
         * @desc 授权失败的回调
         * @param platform 平台名称
         * @param action 行为序号，开发者用不上
         * @param t 错误原因
         */
        @Override
        public void onError(SHARE_MEDIA platform, int action, Throwable t) {
            LoadDialogManager.getInstance().dismiss();
            Toast.makeText(AccountManagement.this, t.getMessage(), Toast.LENGTH_LONG).show();
        }

        /**
         * @desc 授权取消的回调
         * @param platform 平台名称
         * @param action 行为序号，开发者用不上
         */
        @Override
        public void onCancel(SHARE_MEDIA platform, int action) {
            LoadDialogManager.getInstance().dismiss();
            Toast.makeText(AccountManagement.this, "绑定取消了", Toast.LENGTH_LONG).show();
        }
    };

    public boolean isActivityEnable() {
        if (isDestroyed() || isFinishing()) {
            return false;
        }
        return true;
    }

    @Override
    protected void initView() {
        VM = ViewModelProviders.of(this).get(MineFragmVM.class);
        mToolbar = findViewById(R.id.toolbar);
        mTvTitle = findViewById(R.id.tv_title);
        llPhone = findViewById(R.id.llPhone);
        llWx = findViewById(R.id.llWx);
        llQq = findViewById(R.id.llQq);
        llSina = findViewById(R.id.llSina);
        tvPhone = findViewById(R.id.tvPhone);
        tvQq = findViewById(R.id.tvQq);
        tvWx = findViewById(R.id.tvWx);
        tvSina = findViewById(R.id.tvSina);
        initToolBar(mToolbar, true, "");
        mTvTitle.setText("账号管理");
        VM.getMe().observe(this, new Observer<Me>() {
            @Override
            public void onChanged(Me data) {
                if (data != null && data.isStatus()) {
                    setUI(data);
                }
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        VM.getData();
    }

    public void setUI(Me data) {
        Me.DataBean dataData = data.getData();
        if (!TextUtils.isEmpty(dataData.getLogin_user_mobile())) {
            tvPhone.setEnabled(false);
            tvPhone.setTextColor(getResources().getColor(R.color.textColor_main));
            tvPhone.setText(dataData.getLogin_user_mobile());
        } else {
            tvPhone.setTextColor(getResources().getColor(R.color.col_fffb5054));
            tvPhone.setText("未绑定");
        }

        if (dataData.isIs_bind_wechat()) {
            llWx.setEnabled(false);
            tvWx.setTextColor(getResources().getColor(R.color.textColor_main));
            tvWx.setText(dataData.getWechat_nick_name());
        } else {
            tvWx.setTextColor(getResources().getColor(R.color.col_fffb5054));
            tvWx.setText("未绑定");
            llWx.setOnClickListener(view -> {
                UMShareConfig config = new UMShareConfig();
                config.isNeedAuthOnGetUserInfo(true);
                UMShareAPI.get(AccountManagement.this).setShareConfig(config);
                if (UMShareAPI.get(AccountManagement.this).isInstall(AccountManagement.this, SHARE_MEDIA.WEIXIN)) {
                    UMShareAPI.get(AccountManagement.this).getPlatformInfo(AccountManagement.this, SHARE_MEDIA.WEIXIN, authListener);
                } else {
                    Toast.makeText(AccountManagement.this, "您的设备未安装微信客户端", Toast.LENGTH_SHORT).show();
                }
            });

        }
        if (dataData.isIs_bind_qq()) {
            llQq.setEnabled(false);
            tvQq.setTextColor(getResources().getColor(R.color.textColor_main));
            tvQq.setText(dataData.getQq_nick_name());
        } else {
            tvQq.setTextColor(getResources().getColor(R.color.col_fffb5054));
            tvQq.setText("未绑定");
            llQq.setOnClickListener(view -> {
                UMShareConfig config = new UMShareConfig();
                config.isNeedAuthOnGetUserInfo(true);
                UMShareAPI.get(AccountManagement.this).setShareConfig(config);
                LoadDialogManager.getInstance().show(AccountManagement.this);
                if (UMShareAPI.get(AccountManagement.this).isInstall(AccountManagement.this, SHARE_MEDIA.QQ)) {
                    UMShareAPI.get(AccountManagement.this).getPlatformInfo(AccountManagement.this, SHARE_MEDIA.QQ, authListener);
                } else {
                    LoadDialogManager.getInstance().dismiss();
                    Toast.makeText(AccountManagement.this, "您的设备未安装QQ客户端", Toast.LENGTH_SHORT).show();

                }
            });
        }
        if (dataData.isIs_bind_weibo()) {
            llSina.setEnabled(false);
            tvSina.setTextColor(getResources().getColor(R.color.textColor_main));
            tvSina.setText(dataData.getWeibo_nick_name());
        } else {
            tvSina.setTextColor(getResources().getColor(R.color.col_fffb5054));
            tvSina.setText("未绑定");
            llSina.setOnClickListener(view -> {
                UMShareConfig config = new UMShareConfig();
                config.isNeedAuthOnGetUserInfo(true);
                UMShareAPI.get(AccountManagement.this).setShareConfig(config);
                LoadDialogManager.getInstance().show(AccountManagement.this);
                if (UMShareAPI.get(AccountManagement.this).isInstall(AccountManagement.this, SHARE_MEDIA.SINA)) {
                    LoadDialogManager.getInstance().dismiss();
                    UMShareAPI.get(AccountManagement.this).getPlatformInfo(AccountManagement.this, SHARE_MEDIA.SINA, authListener);
                } else {
                    Toast.makeText(AccountManagement.this, "您的设备未安装微博客户端", Toast.LENGTH_SHORT).show();
                    LoadDialogManager.getInstance().dismiss();
                }
            });

        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(AccountManagement.this).onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void initData() {
        super.initData();

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_accountmanagement;
    }

    @Override
    protected void onDestroy() {
        LoadDialogManager.getInstance().dismiss();
        UMShareAPI.get(this).release();
        super.onDestroy();
    }
}
